package com.zky.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.zky.gulimall.product.entity.ProductAttrValueEntity;
import com.zky.gulimall.product.service.ProductAttrValueService;
import com.zky.gulimall.product.vo.AttrRespVo;
import com.zky.gulimall.product.vo.AttrVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zky.gulimall.product.service.AttrService;
import com.zky.common.utils.PageUtils;
import com.zky.common.utils.R;



/**
 * 商品属性
 *
 * @author zky
 * @email 574096634@qq.com
 * @date 2022-08-17 21:28:48
 */
@RestController
@RequestMapping("product/attr")
@Slf4j
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService valueService;

    /**
     * 列表
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType") String attrType){
      try{
          log.info("catelogId:"+catelogId);
          log.info("params:"+ JSON.toJSONString(params));
          log.info("attrType:"+attrType);
          PageUtils page = attrService.queryBaseAttr(params,catelogId,attrType);

          return R.ok().put("page", page);
      }catch (Exception e){
          e.printStackTrace();
          return null;
      }
    }

    /**
     * 规格参数列表
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        try{
            List<ProductAttrValueEntity> entities=valueService.baseAttrListForSpu(spuId);
            return R.ok().put("data", entities);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		try{
            attrService.saveAttr(attr);

            return R.ok();
        }catch (Exception e){
		    e.printStackTrace();
		    return null;
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 批量修改spu信息
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        valueService.updateSpuAttr(spuId,entities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
